<template>
  <div class="regex-tester">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>正则表达式测试工具</h3>
        </div>
      </template>

      <el-form>
        <el-form-item label="正则表达式">
          <div class="regex-input">
            <span class="delimiter">/</span>
            <el-input
              v-model="pattern"
              placeholder="输入正则表达式"
              @input="testRegex"
            />
            <span class="delimiter">/</span>
            <el-input
              v-model="flags"
              class="flags-input"
              placeholder="flags"
              @input="testRegex"
            />
          </div>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="realtime">实时匹配</el-checkbox>
        </el-form-item>

        <el-form-item label="测试文本">
          <el-input
            v-model="testText"
            type="textarea"
            :rows="8"
            placeholder="输入要测试的文本"
            @input="onTextInput"
          />
        </el-form-item>

        <el-form-item>
          <el-button-group>
            <el-button type="primary" @click="testRegex">测试匹配</el-button>
            <el-button @click="clearAll">清空</el-button>
          </el-button-group>
        </el-form-item>

        <template v-if="pattern">
          <el-divider>匹配结果</el-divider>

          <el-form-item v-if="error" class="error-message">
            <el-alert
              :title="error"
              type="error"
              show-icon
            />
          </el-form-item>

          <template v-else>
            <div class="match-info" v-if="matches.length > 0">
              <div class="info-item">
                <span class="label">匹配总数：</span>
                <span class="value">{{ matches.length }}</span>
              </div>
            </div>

            <div class="matches-list" v-if="matches.length > 0">
              <div
                v-for="(match, index) in matches"
                :key="index"
                class="match-item"
              >
                <div class="match-header">
                  匹配项 #{{ index + 1 }}
                </div>
                <div class="match-content">
                  <div class="match-text">
                    <span class="label">完整匹配：</span>
                    <span class="value">{{ match[0] }}</span>
                  </div>
                  <template v-if="match.length > 1">
                    <div
                      v-for="(group, groupIndex) in match.slice(1)"
                      :key="groupIndex"
                      class="match-group"
                    >
                      <span class="label">分组 {{ groupIndex + 1 }}：</span>
                      <span class="value">{{ group }}</span>
                    </div>
                  </template>
                  <div class="match-index">
                    <span class="label">位置：</span>
                    <span class="value">{{ match.index }}</span>
                  </div>
                </div>
              </div>
            </div>

            <el-empty v-else description="没有找到匹配项" />
          </template>

          <el-divider>正则表达式说明</el-divider>

          <div class="regex-explanation">
            <h4>模式说明</h4>
            <ul>
              <li v-if="flags.includes('g')">全局搜索</li>
              <li v-if="flags.includes('i')">忽略大小写</li>
              <li v-if="flags.includes('m')">多行匹配</li>
              <li v-if="flags.includes('s')">允许 . 匹配换行符</li>
              <li v-if="flags.includes('u')">Unicode 模式</li>
              <li v-if="flags.includes('y')">粘性搜索</li>
            </ul>

            <h4>常用语法参考</h4>
            <el-table :data="syntaxReference" border stripe>
              <el-table-column prop="pattern" label="模式" width="150" />
              <el-table-column prop="description" label="说明" />
              <el-table-column prop="example" label="示例" width="180" />
            </el-table>
          </div>
        </template>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const pattern = ref('')
const flags = ref('g')
const testText = ref('')
const matches = ref([])
const error = ref('')
const realtime = ref(true)

const syntaxReference = [
  {
    pattern: '^',
    description: '匹配字符串开始',
    example: '^hello'
  },
  {
    pattern: '$',
    description: '匹配字符串结束',
    example: 'world$'
  },
  {
    pattern: '\\d',
    description: '匹配数字',
    example: '\\d{3}'
  },
  {
    pattern: '\\w',
    description: '匹配字母、数字、下划线',
    example: '\\w+'
  },
  {
    pattern: '\\s',
    description: '匹配空白字符',
    example: '\\s*'
  },
  {
    pattern: '[abc]',
    description: '匹配方括号中的任意字符',
    example: '[aeiou]'
  },
  {
    pattern: '(xyz)',
    description: '分组',
    example: '(\\d{3})'
  },
  {
    pattern: 'x{n}',
    description: '精确匹配n次',
    example: '\\d{4}'
  },
  {
    pattern: 'x{n,}',
    description: '匹配至少n次',
    example: '\\w{2,}'
  },
  {
    pattern: 'x{n,m}',
    description: '匹配n到m次',
    example: '\\d{2,4}'
  }
]

const testRegex = () => {
  matches.value = []
  error.value = ''

  if (!pattern.value || !testText.value) {
    return
  }

  try {
    const regex = new RegExp(pattern.value, flags.value)
    let match

    if (flags.value.includes('g')) {
      while ((match = regex.exec(testText.value)) !== null) {
        matches.value.push(match)
      }
    } else {
      match = regex.exec(testText.value)
      if (match) {
        matches.value.push(match)
      }
    }
  } catch (e) {
    error.value = e.message
  }
}

const clearAll = () => {
  pattern.value = ''
  testText.value = ''
  matches.value = []
  error.value = ''
}

const onTextInput = () => {
  if (realtime.value) {
    testRegex()
  }
}

watch([pattern, flags], () => {
  if (realtime.value) {
    testRegex()
  }
})
</script>

<style scoped>
.regex-tester {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h3 {
  margin: 0;
}

.regex-input {
  display: flex;
  align-items: center;
  gap: 5px;
}

.delimiter {
  font-size: 18px;
  color: #409EFF;
  font-weight: bold;
}

.flags-input {
  width: 100px;
}

.match-info {
  margin-bottom: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.matches-list {
  margin-top: 15px;
}

.match-item {
  background-color: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 10px;
  overflow: hidden;
}

.match-header {
  background-color: #409EFF;
  color: white;
  padding: 8px 15px;
  font-weight: bold;
}

.match-content {
  padding: 15px;
}

.match-text,
.match-group,
.match-index {
  margin-bottom: 8px;
}

.match-text:last-child,
.match-group:last-child,
.match-index:last-child {
  margin-bottom: 0;
}

.label {
  color: #606266;
  margin-right: 5px;
}

.value {
  color: #303133;
  font-family: monospace;
  word-break: break-all;
}

.regex-explanation {
  margin-top: 20px;
}

h4 {
  margin: 15px 0 10px;
  color: #303133;
}

ul {
  margin: 0;
  padding-left: 20px;
  color: #606266;
}

.error-message {
  margin-top: 15px;
}
</style> 